package top.csjss.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.csjss.domain.EbookEntry;
import top.csjss.mapper.EbookEntryDao;
import top.csjss.service.EbookEntryService;

import java.util.List;

/**
 * @Author ztf
 * @Date 16:28 2021/10/6
 **/
@Service
public class EbookEntryServiceImpl implements EbookEntryService {
    @Autowired
    private EbookEntryDao ebookEntryDao;

    @Override
    public List<EbookEntry> findAll() {
        return ebookEntryDao.selectByExample(null);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void insert(EbookEntry ebookEntry) {
        int i = ebookEntryDao.insert(ebookEntry);
        if (i != 1) {
            System.out.println("添加图书失败");
            throw new RuntimeException("添加图书失败");
        }
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void updateById(EbookEntry ebookEntry) {
        int i = ebookEntryDao.updateByPrimaryKeySelective(ebookEntry);
        if (i != 1) {
            System.out.println("修改图书失败");
            throw new RuntimeException("修改图书失败");
        }
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void deleteById(Integer id) {
        int i = ebookEntryDao.deleteByPrimaryKey(id);
        if (i != 1) {
            System.out.println("删除图书失败");
            throw new RuntimeException("删除图书失败");
        }
    }

    @Override
    public PageInfo<EbookEntry> findByCategoryIdAndPage(Integer page, Integer rows, Integer CategoryId) {
        PageHelper.startPage(page, rows);

        List<EbookEntry> ebookEntries = ebookEntryDao.findByCategoryId(CategoryId);

        PageInfo<EbookEntry> pageInfo=new PageInfo<>(ebookEntries);

        return pageInfo;
    }
}
